import requests
from bs4 import BeautifulSoup
import pandas as pd
import time
import os
from view import areaView, priceView, distanceView, timeView


def getUrls():
	urls = []  # 列表
	baseUrl = "https://www.che168.com/china/a0_0msdgscncgpi1ltocsp"
	for i in range(1, 6):  # 限制5页
		url = "{}{}exx0/".format(baseUrl, i)
		urls.append(url)  # 把生成好的 url 添加到列表 urls 当中方便返回
	return urls


headers = {
	"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0"}


def gethtmltext(url):
	try:
		r = requests.get(url, headers=headers)  # 发送请求
		r.raise_for_status()
		return r.text
	except:
		return ""


def ParsePage(html):
	if html == '':
		return
	soup = BeautifulSoup(html, features="html.parser")
	# "名称", "里程", "时间", "地址", "会员", "价格", "原价", "过户次数"
	info = soup.select("div .cards-bottom")
	# 定义一个列表 用于存储一页的数据
	data = []
	# 格式打印
	for item in info:
		name = item.contents[1].text
		# 0.01万公里／2020-12／北京／1年黄金会员
		msgMix = str(item.contents[3].text).split("／")
		if len(msgMix) != 4:
			break
		price = item.contents[5].contents[0].contents[0].text
		if price == '抢购价':
			continue
		t = msgMix[1].split("-")[0]
		if t == '未上牌':
			t = 2024

		data.append([name, msgMix[0].replace("万公里", ""), t, msgMix[2], msgMix[3], price])
	return data


def getData():
	if os.path.exists('data.csv'):
		os.remove('data.csv')
	# 添加一个标题行
	columns = ["名称", "里程", "时间", "地址", "会员", "价格"]
	df = pd.DataFrame([], columns=columns)

	df.to_csv("data.csv", mode="a", header=True, index=False, encoding="utf-8")
	urls = getUrls()  # 返回值是一个列表
	for i in range(len(urls)):
		# 用 html 保存获取到的网页的所有信息
		html = gethtmltext(urls[i])
		# 解析每一页的数据
		data = ParsePage(html)
		# 把数据写入文件
		df = pd.DataFrame(data)  # DataFrame 数据帧 二维表格结构
		df.to_csv("data.csv", mode="a", index=False, header=False, encoding="utf-8_sig")  # 追加
		print("第{}页写入完毕！".format(i + 1))
		time.sleep(2)  # 延时等待2秒


def draw():
	if os.path.exists('data.csv'):
		df = pd.read_csv("data.csv")
		df = df.dropna()
		areaView(df)
		priceView(df)
		distanceView(df)
		timeView(df)


if __name__ == "__main__":
	getData()
	draw()
